package paket;


import admin.gameInterface;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mete
 */
public class NetworkConnection {
    Socket connect;
    String host;
    int port;
    gameInterface PostGame;
    ObjectOutputStream serverOutput;
    ObjectInputStream ServerInput;
    public NetworkConnection() {
        host = "127.0.0.1";
        port = 8000;
        Connection();
    }
    
    public void Connection()
    {
        try{
        connect = new Socket("localhost",8000);
        }catch(Exception e){
            System.out.println(e);
        }
        if (connect != null){
            try {
                serverOutput = new ObjectOutputStream(connect.getOutputStream());
                ServerInput = new ObjectInputStream(connect.getInputStream());
            } catch (IOException ex) {
                //Logger.getLogger(NetworkConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
    }
    public gameInterface Readgame() {
    
   // while (true){
            try {
                if (ServerInput != null){
                    PostGame = (gameInterface) ServerInput.readObject();
                    return PostGame;
                }
            } catch (Exception ex) {
                //Logger.getLogger(NetworkConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (PostGame.status == 0){
                    return null;
            }
            return null;
           
    }//}
    public void WriteGame(gameInterface game){
        try {
            if (game != null){
                serverOutput.writeObject(game);
            }
        } catch (IOException ex) {
            Logger.getLogger(NetworkConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
